#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "maz/assert.h"
#include "riff_wav.h"

uint32_t get_file_size(char *file_name);

int dlvl = DBUG;

int main(int argc, char **argv)
{
    int ret = 0;
    int format = 0;
    int samplerate = 0;
    int bitdepth = 0;
    int channel =0 ;
    int len = 0;
    WAVE_INFO info = { 0 };
    WAVE_HEADER header = { 0 };
    const char s[2] = ".";

    char        iname[32];
    char       *suffix;
    FILE       *ifile       = NULL;
    uint8_t    *ibuf        = NULL;
    uint32_t    isize       = 0;
    int         ircnt       = 0;

    char       *oname;
    FILE       *ofile       = NULL;
    int         owcnt       = 0;

    /* 打印函数使用方法 */
    if (argc != 6)
    {
        printf("usage   : %s <pcm file name> <format> <samplerate> <bitdepth> <channel>\n", argv[0]);
        printf("eg      : %s dayu_8k_1ch_s16le.pcm   1 8000  16 1\n", argv[0]);
        printf("eg      : %s dayu_8k_1ch_s16le.pcm   1 8000  16 1 --- error\n", argv[0]);
        printf("eg      : %s dayu_16k_1ch_s16le.pcm  1 16000 16 1\n", argv[0]);
        printf("eg      : %s dayu_16k_1ch_s16be.pcm  1 16000 16 1 --- error\n", argv[0]);
        printf("eg      : %s dayu_8k_1ch_s16le.alaw  6 8000  8  1\n", argv[0]);
        printf("eg      : %s dayu_8k_1ch_s16le.mulaw 7 8000  8  1\n", argv[0]);
        printf("\n");
        printf("format     :\n");
        printf("     1     : PCM\n");
        printf("     6     : G711A\n");
        printf("     7     : G711U\n");
        printf("samplerate :\n");
        printf("     8000  : 8KHz\n");
        printf("     16000 : 16KHz\n");
        printf("bitdepth   :\n");
        printf("     8     : 8bit\n");
        printf("     16    : 16bit\n");
        printf("channel    :\n");
        printf("     1     : mono\n");
        printf("     2     : stereo\n");
        printf("\n");
        return -1;
    }

    sscanf(argv[2], "%d", &format);
    dmsg(DBUG, "foramt      = %d\n", format);

    sscanf(argv[3], "%d", &samplerate);
    dmsg(DBUG, "samplerate  = %d\n", samplerate);

    sscanf(argv[4], "%d", &bitdepth);
    dmsg(DBUG, "bitdepth    = %d\n", bitdepth);

    sscanf(argv[5], "%d", &channel);
    dmsg(DBUG, "channel     = %d\n", channel);

    /* 解析输入参数得到文件名 */
    strcpy(iname, argv[1]);
    oname = strtok(argv[1], s);
    sprintf(oname, "%s.wav", oname);
    dmsg(DBUG, "iname       = %s\n", iname);
    dmsg(DBUG, "oname       = %s\n", oname);

    /* 获取输入文件大小, 计算输出文件大小 */
    len = sizeof(WAVE_HEADER);
    isize = get_file_size(iname);
    dmsg(DBUG, "isize       = %d\n", isize);

    /* 申请数据内存 */
    ibuf = (uint8_t *)malloc(isize);
    memset(ibuf, 0, isize);

    /* 读取输出数据 */
    ifile = fopen(iname, "r");
    ASSERT_RETVAL(NULL == ifile, -1, "err: can not open %s file", iname);

    ircnt = fread(ibuf, 1, isize, ifile);
    ASSERT_RETVAL(ircnt != isize, ret, "err: fread, rcnt = %d", ircnt);

    fclose(ifile);

    info.samplerate = samplerate;
    info.bitdepth = bitdepth;
    info.channel = channel;
    info.format = format;
    info.length = isize;
    ret = riff_wav_header_pack(&info, &header);
    ASSERT_RETVAL(ret, -1, "err: riff_wav_header_pack(header, %d, %d, %d, %d, %d)", samplerate, bitdepth, format, channel, isize);
    dmsg(DBUG, "h.sample    = %d\n", header.subchunk1.samplerate);
    dmsg(DBUG, "h.bit       = %d\n", header.subchunk1.bitdepth);
    dmsg(DBUG, "h.chan      = %d\n", header.subchunk1.channel);
    dmsg(DBUG, "h.byte      = %d\n", header.subchunk1.byterate);
    dmsg(DBUG, "h.align     = %d\n", header.subchunk1.blockalign);
    dmsg(DBUG, "h.fmt       = %d\n", header.subchunk1.format);

    /* 输出到文件 */
    ofile = fopen(oname, "wb");
    ASSERT_RETVAL(NULL == ofile, -1, "err: can not open %s file", oname);

    owcnt = fwrite(&header, 1, len, ofile);
    ASSERT_RETVAL(owcnt != len, ret, "err: fwrite, wcnt = %d", owcnt);

    owcnt = fwrite(ibuf, 1, isize, ofile);
    ASSERT_RETVAL(owcnt != isize, ret, "err: fwrite, wcnt = %d", owcnt);

    fclose(ofile);
    free(ibuf);

    printf("PCM %s packet to wav finish!\n", argv[1]);

    return 0;
}

uint32_t get_file_size(char *file_name)
{
    struct stat buf;
    if(stat(file_name, &buf) < 0)
    {
        return 0;
    }
    return (uint32_t)buf.st_size;
}


